/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.Impl;

import com.startup.musicstore.app.facade.Facade;
import com.startup.musicstore.model.Customer;
import com.startup.musicstore.model.CustomerInvoice;
import com.startup.musicstore.model.Orders;
import com.startup.musicstore.services.PendingService;
import com.startup.musicstore.services.crud.CustomerCrudService;
import com.startup.musicstore.services.crud.OrderCrudService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author aljay19
 */
@Service("pendingService")
@Transactional
public class PendingServiceImpl implements PendingService {

    private Facade data = new Facade();
    private OrderCrudService orderCrudService;
    private CustomerCrudService customerCrudService;
    
    @Override
    public List<Orders> listPendingOrders() {
        
        orderCrudService = data.getOrderCrudService();
        List<Orders> allOrders = orderCrudService.findAll();
        List<Orders> pendingOrders = new ArrayList<Orders>();
        for (Orders o : allOrders) {
            CustomerInvoice customerInvoice = o.getCustomerInvoice();
            if (customerInvoice.getInvoiceStatus().equalsIgnoreCase("Unpaid")) {
                pendingOrders.add(o);
            }
        }
        return pendingOrders;
    }

    @Override
    public List<Customer> listPendingCustomers() {
        
        customerCrudService = data.getCustomerCrudService();
        List<Customer> allCustomers = customerCrudService.findAll();
        List<Customer> pendingCustomers = new ArrayList<Customer>();
        for (Customer c : allCustomers) {
            List<Orders> orders = c.getOrders();
            for (Orders o : orders) {
                CustomerInvoice customerInvoice = o.getCustomerInvoice();
                if (customerInvoice.getInvoiceStatus().equalsIgnoreCase("Unpaid")) {
                    pendingCustomers.add(c);
                }
            }
        }
        return pendingCustomers;
    }
}
